import dayjs from 'dayjs';
import { useTable, LIST_TYPE } from '@crud/index';
import { toolOptions2Map, toolGetDefaultTwoWeekDate } from '@/utils';

// 付款方式
export const payTypeOptions = [
  { label: '不限', value: '' },
  { label: '微信', value: 2 },
  { label: '支付宝', value: 1 },
  { label: '苹果内购', value: 3 },
  { label: '对公转账', value: 4 },
  { label: '花呗分期', value: 8 }
];

// 订单类型
export const orderTypeOptions = [
  { label: '不限', value: '' },
  { label: '已付款', value: 200 },
  { label: '已退款', value: -300 }
];

// 是否是测试订单
export const hasTestOrderOptions = [
  { label: '不限', value: -1 },
  { label: '包含测试订单', value: true },
  { label: '不包含测试订单', value: false }
];

/** 设备枚举 */
export const platformOptions = [
  { label: 'PC', value: 1 },
  { label: '小程序', value: 2 },
  { label: '安卓App', value: 3 },
  { label: '苹果App', value: 4 }
];

// 订单类型 1-钻石会员 2-黄金会员 3-联合会员
export const memberOptions = [
  { label: '钻石会员', value: 1 },
  { label: '黄金会员', value: 2 },
  { label: '白银会员', value: 4 },
  { label: '联合会员', value: 3 }
];

// 购买类型
export const buyOptions = [
  { label: '月卡', value: 1 },
  { label: '季卡', value: 2 },
  { label: '年卡', value: 3 }
];

export const payTypeMap = toolOptions2Map(payTypeOptions);
export const orderTypeMap = toolOptions2Map(orderTypeOptions);
export const platformMap = toolOptions2Map(platformOptions);

// 表格
export const useTableList = (options: any = {}) => {
  const columns = [
    { label: '流水单号', prop: 'order_no', width: 300, type: 'copy', fixed: 'left' },
    { label: '订单手机号', prop: 'mobile', width: 120 },
    { label: '购买类型', prop: 'subject', width: 200 },
    { label: '支付金额', prop: 'total_fee', width: 120, align: 'center', type: 'priceCent' },
    { label: '充值方式', prop: 'terminal_type', width: 120, align: 'center', render: (s: any) => platformMap[s.row.terminal_type] },
    { label: '支付方式', prop: 'pay_type', width: 120, align: 'center', render: 'pay_type' },
    { label: '支付时间', prop: 'complete_time', width: 180, type: 'dateTime' },
    { label: '订单生效时间', prop: 'start_time', width: 180, type: 'dateTime' },
    { label: '1级渠道', prop: 'utm_source', width: 160, ellipsis: true, showOverflowTooltip: true, render: (s: any) => s.row.utm_source ?? '-' },
    { label: '2级渠道', prop: 'utm_term', width: 160, ellipsis: true, showOverflowTooltip: true, render: (s: any) => s.row.utm_term ?? '-' },
    { label: '订单备注', prop: 'remark', minWidth: 200, ellipsis: true, showOverflowTooltip: true, render: (s: any) => s.row.remark ?? '-' },
    { label: '订单状态', prop: 'trade_status', width: 120, align: 'center', render: (s: any) => orderTypeMap[s.row.trade_status] },
    { label: '操作', align: 'center', width: 120, render: 'actions', fixed: 'right' }
  ];

  return useTable({
    ...options,
    type: LIST_TYPE.AGENCY,
    params: {
      _dateRange: toolGetDefaultTwoWeekDate,
      mobile: undefined, // 手机号
      subject: undefined, // 购买类型
      pay_type: undefined, // 付款方式
      trade_status: undefined, // 订单状态
      utm_source: undefined, // 一级渠道
      utm_term: undefined, // 二级渠道
      from_dt: undefined, // 订单开始时间(含),单位秒
      to_dt: undefined, // 订单结束时间(不含),单位秒
      has_test_order: -1 // 是否含测试订单
    },
    columns: columns,
    getParams: ({ _dateRange = [], has_test_order, ...params }) => {
      if (_dateRange && _dateRange.length) {
        params.from_dt = dayjs(_dateRange[0]).startOf('day').format('X');
        params.to_dt = dayjs(_dateRange[1]).endOf('day').format('X');
      }

      if (has_test_order !== -1 && has_test_order !== undefined) {
        params.has_test_order = has_test_order;
      }
      return params;
    },
    getList: options.getList
  });
};
